/**
 * Created by Administrator on 2015/4/19.
 */

var express = require('express');
var router = express.Router();
var http = require('http').Server(router);
router.get('/', function(req, res, next) {
    res.render('mychat1');
});
var io = require('socket.io')(http);
io.set('log level', 1);
var users = {};
io.sockets.on('connection', function (socket) {
    io.sockets.emit('connect',{hell:'boy'});
    socket.on('private message', function (from,to,msg) {
        console.log('I received a private message by ', from, ' say to ',to, msg);
        if(to in users){
            users[to].emit('to'+to,{mess:msg});
        }
    });
    socket.on('new user',function(data){
        if(data in users){

        }else{
            var nickname = data;
            users[nickname]= socket;
        }
        console.info(users);
    });
    socket.on('disconnect', function () {
        io.sockets.emit('user disconnected');
    });
});
var server = http.listen(4000, function() {
    console.log('start at port:' + server.address().port);
});
module.exports = router;
